'use strict';

/* eslint-disable */
$(function () {
  function  addParama (key, val) {
    var search = window.location.search;
    var url = '';
    if (search.indexOf('?') != -1) {
      search = search.substring(1);
      var search_arr = search.split('&');
      var url_arr = [];
      var exists = 0;
      for (var i = 0; i < search_arr.length; i++) {
        if (!search_arr[i])
          continue;
        var temp = search_arr[i].split('=');
        if (key == temp[0]) {
          exists = 1;
          url_arr.push(key + '=' + val);
        } else {
          if (temp[0] != 'page' && temp[0] != 'token') {
            url_arr.push(search_arr[i]);
          }
        }
      }
      if (!exists)
        url_arr.push(key + '=' + val);
      url = window.location.pathname + '?' + url_arr.join('&');
    } else {
      url = window.location.pathname + '?' + key + '=' + val;
    }
    window.location.href = url;
  };
  function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
      return decodeURIComponent(r[2]);
    }
    return null;
  }
  var id = getQueryString("id");
  if(id) {
    $('#body-wrapper').hide();
    $('#body-wrapper__detail').show();
    var req = $.ajax({
      url: "http://39.106.171.39:8011/mbl/Research/getResearchItem",
      method: "GET",
      data: {
        researchid: id,
      },
      dataType: "json",
    });
    var req_list = $.ajax({
      url: "http://39.106.171.39:8011/mbl/Research/getResearchList",
      method: "GET",
      data: {
        page: 1,
        pagesize: 10,
      },
      dataType: "json",
    });
    req_list.done(function (data) {
      var researchList = data.rows;
      var research_sidebar = $('#research_sidebar');
      for (let i = 0, len = researchList.length; i < len; i++ ) {
        var sidebarItem = $('<span class="my-btn sidebar-item pointer"></span>').text(researchList[i].name);
        sidebarItem.click(function() {
          addParama("id", researchList[i].researchid);
        });
        research_sidebar && research_sidebar.append(sidebarItem);
      }
    });
    req.done(function (data) {
      var lists = data.articleResultList;
      var research_intro = $('#research-detail__intro');
      var img = $('<img>').attr('src', data.picture);
      var detailElem = $('<div class="detail"></div>');
      var h2 = $('<h2></h2>').text(data.name);
      var p = $('<p></p>').text(data.introduction);
      detailElem.append(h2).append(p);
      research_intro.append(img).append(detailElem);

      var researchDetail = $('#research-detail');
      for (var i = 0, len = lists.length; i < len; i++) {
        var item = $('<section class="publication-item"></section>');
        var h3 = $('<h3></h3>').text(lists[i].title);
        var p1 = $('<p></p>').text(lists[i].author);
        var p2 = $('<p></p>');
        var journalName = $('<span></span>').text(lists[i].journalname);
        var doi = $('<span class="mark"></span>').text(lists[i].doi);
        p2.append(journalName).append(': DOI: ').append(doi);

        item.append(h3).append(p1).append(p2);
        researchDetail.append(item);
      }
    });
  } else {
    $('#body-wrapper').show();
    $('#body-wrapper__detail').hide();
    var req = $.ajax({
      url: "http://39.106.171.39:8011/mbl/Research/getResearchList",
      method: "GET",
      data: {
        page: 1,
        pagesize: 10,
      },
      dataType: "json",
    });
    req.done(function (data) {
      var researchList = data.rows;
      var bodyWrapper = $('#body-wrapper');
      for (let i = 0, len = researchList.length; i < len; i++ ) {
        var item = $('<section class="subject"></section>');
        var divItem = $('<div class="subject-wrapper"></div>');
        var img = $("<img />").attr("src", researchList[i].picture);
        var introWrapper = $('<div class="subject-intro"></div>');
        var h2 = $("<h2></h2>").text(researchList[i].name);
        var p = $("<p></p>").text(researchList[i].introduction);
        var span = $('<span class="btn" id="research_detail">Read More</span>');
        span.click(function() {
          addParama("id", researchList[i].researchid);
        });
        introWrapper.append(h2).append(p).append(span);
        divItem.append(img).append(introWrapper);
        item.append(divItem);

        bodyWrapper.append(item);
      }
    });
  }
});
